package bl.commodity;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

import po.AccountBookPo;
import bl.document.StockSendingDocumentBL;
import bl.document.ImportAndReturnDocumentBL;
import bl.document.SaleAndReturnDocumentBL;
import blservice.commodity.StockBLService;
import vo.CommodityVo;
import vo.DocumentVo;
import vo.StockVo;
import vo.ImportAndReturnDocumentVo;
import vo.SaleAndReturnDocumentVo;
import vo.StockSendingDocumentVo;
import db.Database;
import db.commodity.CommodityDB;
import db.document.StockSendingDocumentDB;
import db.document.ImportAndReturnDocumentDB;
import db.document.SalesAndReturnDocumentDB;
import db.history.SystemLogDB;
import dbservice.commodity.CommodityDBService;
import dbservice.document.DocumentDBService;
import dbservice.history.SystemLogDBService;

public class StockBL extends UnicastRemoteObject implements StockBLService{
	public static void main(String[] args) throws SQLException, RemoteException{
		Database.iniDatabase("127.0.0.1",3306,"seac2?useUnicode=true&characterEncoding=utf-8","developer", "19960623");
		AccountBookPo ab=new AccountBookPo();
		ab.setName("accountbook");
		CommodityDBService commodityDB=new CommodityDB(ab);
		SystemLogDBService log=new SystemLogDB();
		StockBL stockbl=new StockBL(commodityDB,log,ab);
		List<StockVo> stock_list=stockbl.search("2014-09-09", "2014-12-25");
		for(int i=0;i<stock_list.size();i++){
			System.out.println("商品："+stock_list.get(i).getCommodityName()+"数量："+stock_list.get(i).getStock_num()+" "+"销售数量："+stock_list.get(i).getSaleNum()+" "+"销售金额："+stock_list.get(i).getSale_money()+" "+"进货数量："+stock_list.get(i).getImportNum()+" "+"进货金额："+stock_list.get(i).getImport_money()+" "+"出库数量："+stock_list.get(i).getOut_repertory()+" "+"出库金额："+stock_list.get(i).getOut_repertory_money()+" 入库数量:"+stock_list.get(i).getIn_repertory()+" 入库金额："+stock_list.get(i).getIn_repertory_money());
		}
		
		
		
	}
	
	
	

	private static final long serialVersionUID = -6308041739655816491L;
	CommodityDBService commodityDB;
	SystemLogDBService log;
	AccountBookPo ab;

	public StockBL(CommodityDBService commodityDB,SystemLogDBService log,AccountBookPo ab) throws RemoteException{
		this.commodityDB = commodityDB;
		this.ab=ab;
		this.log=log;
	}

	@Override
	public List<StockVo> search(String date_from, String date_to)              //库存查看
			throws RemoteException {
		List<StockVo> stock=new ArrayList<StockVo>();
		
		DateFormat df = new SimpleDateFormat ("yyyy-MM-dd");     
	    Date from_date=null;Date to_date=null;
		try {
				from_date =df.parse(date_from);
				to_date=df.parse(date_to);
			} catch (java.text.ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		
		
        DocumentVo from=new DocumentVo();
        from.setDate(from_date);
        from.setType(false);     //销售单，进货单
        DocumentVo to=new DocumentVo();
        to.setDate(to_date);
       try {
    	   CommodityDBService cmdb=new CommodityDB(ab);
       	CommodityBL cmbl=new CommodityBL(cmdb,log);
       	List<CommodityVo> cm_list=cmbl.getAll();
       	for(int i=0;i<cm_list.size();i++){
       		CommodityVo cm_vo=new CommodityVo();
       		StockVo stock_vo=new StockVo();
       		cm_vo=cm_list.get(i);
       		//String commodityID=cm_vo.get编号();
       		String commodityModel=cm_vo.get型号();
       		String commodityName=cm_vo.getName();
       		int commodityNum=cm_vo.getstocknum();
       		long stock_average=cm_vo.get库存均价();
       		stock_vo.setCommodityName(commodityName);
       		stock_vo.setCommodityModel(commodityModel);
       		
       		stock_vo.setSaleNum(0);                  //销售数量
       		stock_vo.setSale_money(0);               //销售金额
       		
       		stock_vo.setImportNum(0);                //进货数量
       		stock_vo.setImport_money(0);             //进货金额
       		
       	
       		
       		
       		stock_vo.setIn_repertory(0);             //入库数量
       		stock_vo.setIn_repertory_money(0);       //入库金额
       		
       		stock_vo.setOut_repertory(0);            //出库数量
       		stock_vo.setOut_repertory_money(0);      //出库金额
       		
       		stock_vo.setStock_average_price(stock_average);
       		stock_vo.setStock_num(commodityNum);
       		
       		stock.add(stock_vo);
       	}
    	   
    	   
    	   
    	    DocumentDBService saledb=new SalesAndReturnDocumentDB(ab);
     		SaleAndReturnDocumentBL salebl=new SaleAndReturnDocumentBL(saledb,log);
     		List<DocumentVo> vo1=salebl.get(from, to);       //得到销售单vo
     		
     		
     		
     		DocumentDBService importdb=new ImportAndReturnDocumentDB(ab);
     		ImportAndReturnDocumentBL importbl=new ImportAndReturnDocumentBL(importdb,log);
     		List<DocumentVo> vo_import=importbl.get(from, to);    //得到进货单
     		     		
     		
     		
     		DocumentDBService giftdb=new StockSendingDocumentDB(ab);
     		StockSendingDocumentBL giftbl=new StockSendingDocumentBL(giftdb,log);
     		List<DocumentVo> vo_gift=giftbl.get(from, to);
     		
     		
     		from.setType(true);
     		List<DocumentVo> vo2=salebl.get(from, to);       //得到销售退货单vo,入库
        	List<DocumentVo> vo_import_return=importbl.get(from, to);      //得到进货退货单，出库
        	
     		
     		
     		
     		
     		
        	for(int j=0;j<stock.size();j++){
               for(int i=0;i<vo1.size();i++){                  //获得销售数量，销售金额
            	   for(int k=0;k<((SaleAndReturnDocumentVo)vo1.get(i)).get出货商品清单().size();k++){
        		    if(stock.get(j).getCommodityName().equals(((SaleAndReturnDocumentVo)vo1.get(i)).get出货商品清单().get(k).get商品名称())){
        		    	stock.get(j).setSaleNum(stock.get(j).getSaleNum()+((SaleAndReturnDocumentVo)vo1.get(i)).get出货商品清单().get(k).get数量());
        		    	stock.get(j).setSale_money(stock.get(j).getSale_money()+((SaleAndReturnDocumentVo)vo1.get(i)).get出货商品清单().get(k).get金额());
        		    }
        			   
        		   }
        	   }
               stock.get(j).setOut_repertory(stock.get(j).getSaleNum());
			   stock.get(j).setOut_repertory_money(stock.get(j).getSale_money());
              
			   for(int i=0;i<vo_import.size();i++){                           //获得进货数量，进货金额
            	   for(int k=0;k<((ImportAndReturnDocumentVo)vo_import.get(i)).get入库商品列表().size();k++){
            		   if(stock.get(j).getCommodityName().equals(((ImportAndReturnDocumentVo)vo_import.get(i)).get入库商品列表().get(k).get商品名称())){
            			   stock.get(j).setImportNum(stock.get(j).getImportNum()+((ImportAndReturnDocumentVo)vo_import.get(i)).get入库商品列表().get(k).get数量());
            			   stock.get(j).setImport_money(stock.get(j).getImport_money()+((ImportAndReturnDocumentVo)vo_import.get(i)).get入库商品列表().get(k).get金额());
            		   }
            	   }
               }
            
               stock.get(j).setIn_repertory(stock.get(j).getImportNum());
		       stock.get(j).setIn_repertory_money(stock.get(j).getImport_money());
            
             for(int i=0;i<vo2.size();i++){                                  //入库数量=进货数量+销售退货数量
            	   for(int k=0;k<((SaleAndReturnDocumentVo)vo2.get(i)).get出货商品清单().size();k++){
        		    if(stock.get(j).getCommodityName().equals(((SaleAndReturnDocumentVo)vo2.get(i)).get出货商品清单().get(k).get商品名称())){
        		    	stock.get(j).setIn_repertory(stock.get(j).getIn_repertory()+((SaleAndReturnDocumentVo)vo2.get(i)).get出货商品清单().get(k).get数量());
        		    	stock.get(j).setIn_repertory_money(stock.get(j).getIn_repertory_money()+((SaleAndReturnDocumentVo)vo2.get(i)).get出货商品清单().get(k).get金额());
        		    }
        			   
        		   }
        	   }
           
            	
            
              
            
            for(int i=0;i<vo_import_return.size();i++){                           //出库数量=销售数量+进货退货数量+库存赠送数量
            	   for(int k=0;k<((ImportAndReturnDocumentVo)vo_import_return.get(i)).get入库商品列表().size();k++){
            		   if(stock.get(j).getCommodityName().equals(((ImportAndReturnDocumentVo)vo_import_return.get(i)).get入库商品列表().get(k).get商品名称())){
            			   stock.get(j).setOut_repertory(stock.get(j).getOut_repertory()+((ImportAndReturnDocumentVo)vo_import_return.get(i)).get入库商品列表().get(k).get数量());
            			   stock.get(j).setOut_repertory_money(stock.get(j).getOut_repertory_money()+((ImportAndReturnDocumentVo)vo_import_return.get(i)).get入库商品列表().get(k).get金额());
            		   }
            	   }
               }
            
            
              for(int i=0;i<vo_gift.size();i++){             //出库数量+=库存赠送数量
            	  if(stock.get(j).getCommodityName().equals(((StockSendingDocumentVo)vo_gift.get(i)).get商品名称())){
            		  stock.get(j).setOut_repertory(stock.get(j).getOut_repertory()+((StockSendingDocumentVo)vo_gift.get(i)).getNum());
            		  stock.get(j).setOut_repertory_money(stock.get(j).getOut_repertory_money()+stock.get(j).getStock_average_price()*((StockSendingDocumentVo)vo_gift.get(i)).getNum());
            		  
            	  }
              }
               
        	
        	}
        	} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return stock;
	}

	

	

}
